var userList = function () {

    var handler = function () {
        layui.use(['table', 'layer', 'form', 'upload'], function () {
            var table = layui.table,
                layer = layui.layer,
                form = layui.form,
                upload = layui.upload;

            //初始化表格
            table.render({
                elem: "#infoList"
                , url: "/user/listAll"
                , id: "tableInfo"
                , method: "get"
                , response: {
                    statusCode: 200 //规定成功的状态码，默认：0
                }
                , parseData: function (res) {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "data": res.data.data //解析数据列表
                    }
                }
                , cols: [[
                    {type: "checkbox", align: 'center'}
                    , {field: "id", title: "编号", align: 'center', sort: true}
                    , {field: "number", title: "编码", align: 'center'}
                    , {field: "name", title: "姓名", align: 'center'}
                    , {field: "age", title: "年龄", align: 'center'}
                    , {title: '操作', toolbar: '#barDemo', align: 'center'}
                ]]
            });

            //重新加载
            function reload() {
                table.reload("tableInfo", {
                    where: {
                        name: $("#userList").val(),
                    }
                })
            };

            //查询n
            $("#find").click(function () {
                reload();
            });

            //下拉
            /*$.get('/synchronys/A/selectName.do', function (res) {
                if (res.resultCode == 200) {
                    var datas = res.resultData.data;
                    var options = "<option value='' >请选择</option>";
                    $.each(datas, function (index, element) {
                        options += "<option value='" + element.name + "'>" + element.name + "</option>";
                    });
                    $("#userList").html(options);
                    form.render('select');
                }
            })*/
            //导入
            upload.render({
                elem: '#import'
                , url: '/synchronys/A/importFile.do'
                , accept: 'file' //普通文件
                , exts: 'xls|xlsx' //只允许上传excel
                , done: function (res) {
                    if (res.resultCode == 200) {
                        var datas = res.resultData.data;
                        layer.msg("导入成功，共导入" + datas + "条数据")
                    }
                }
            });
            //导出
            $("#export").click(function () {
                window.location.href = "/synchronys/A/exportFile.do";
            });
            //单个删除
            table.on('tool(test)', function (obj) {
                var data = obj.data//获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
                console.info(data);
                if (layEvent === 'del') {
                    layer.confirm('确认要删除吗？', function (index) {
                        //发异步删除数据
                        $.ajax({
                            url: "/user/delete",
                            type: "post",
                            data: JSON.stringify(data.id),
                            dataType: "json",
                            contentType: 'application/json',
                            success: function (result) {
                                if (result.code == 200) {
                                    layer.msg('删除成功');
                                    reload();
                                } else {
                                    layer.msg("删除失败")
                                }
                            }
                        });
                        //同步删除
                        /*$.ajax({
                            url: "/synchronys/B/delete.do",
                            type: "post",
                            data: JSON.stringify(data.id),
                            dataType: "json",
                            contentType: 'application/json',
                            success: function (result) {
                                if (result.resultCode != 200) {
                                    layer.msg('同步删除失败');
                                }
                            }
                        });*/
                    })
                } else if (layEvent === "edit") {
                    x_admin_show('编辑', '/page/edit?name=' + data.name
                        + "&number=" + data.number
                        + "&age=" + data.age
                        + "&sex=" + data.sex
                        + "&id=" + data.id)
                }

            })
        });
    };
    return {
        init: function () {
            handler();
        }
    }
}();